﻿@using Covenant.Models.Listeners
@model HostedFile
@{
    Layout = "../Listener/_ListenerLayout";
}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">Create HostedFile</h1>
</div>

<form id="file-submit-form" asp-action="Create">
    <div asp-validation-summary="All" class="text-danger"></div>
    <input asp-for="Id" value="@Model.Id" type="hidden">
    <input asp-for="ListenerId" value="@Model.ListenerId" type="hidden">
    <div class="form-row">
        <div class="form-group col-md-4">
            <label asp-for="Path"></label>
            <input asp-for="Path" class="form-control">
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label asp-for="Content">File</label>
            <input asp-for="Content" type="file" class="form-control-file">
            <input id="set-content-filler" name="Content" class="form-control file-filler" type="hidden" readonly disabled>
        </div>
    </div>
    <button type="button" id="submit-file-button" class="btn btn-primary"><span data-feather="plus"></span> Create</button>
</form>

@section Scripts
{
    <script type="text/javascript">
        $('#submit-file-button').click(function () {
            var filecontrol = $("input.form-control-file")[0];
            var file = filecontrol.files[0];
            var fr = new FileReader();
            fr.onload = receivedFileBuffer;
            fr.readAsDataURL(file);

            function receivedFileBuffer() {
                $("input.file-filler").val(fr.result.split(",").pop()).prop("disabled", false);
                $("input.form-control-file").prop("disabled", true);
                $("#file-submit-form").submit();
            }
        });
    </script>
}